package com.zuipin.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zuipin.entity.SysMenu;
import com.zuipin.framework.annotation.Inject;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.service.ISysMenuService;
import com.zuipin.util.DateUtils;
import com.zuipin.util.Pagination;
import com.zuipin.vo.MenuTreeNode;
import com.zuipin.vo.ModityPermissionVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 菜单Controller
 * 
 * @ClassName: SysMenuController
 * @Description: TODO
 * @author robot
 * @date 2017年09月147日 09:21:09
 */
@Api(value = "菜单", description = "菜单")
@RestController
@RequestMapping(value = "/sysMenu")
public class SysMenuController {
	@Resource
	public ISysMenuService sysMenuService;
	
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	@ApiOperation(value = "根据主键查询菜单", notes = "查询菜单")
	public Result<SysMenu> getSysMenuById(@ApiParam(required = true, name = "id", value = "主键 自增长") @RequestParam Long id) {
		SysMenu sysMenu = sysMenuService.findById(id);
		Result<SysMenu> result = new Result<SysMenu>();
		if (sysMenu != null) {
			result.setData(sysMenu);
			result.setErrorCode(ErrorCode.SUCCESS);
		} else {
			result.setErrorCode(ErrorCode.ELSE);
		}
		return result;
	}
	
	/*@RequestMapping(value = "/addSysMenu", method = RequestMethod.POST)
	@ApiOperation(value = "6期-zhuangjiajian-系统新增菜单", notes = "系统新增菜单")
	@Valids({
			
			@Valid(name = "sysMenu.sysId", maxLength = 19), @Valid(name = "sysMenu.menuName", maxLength = 120), @Valid(name = "sysMenu.menuCode", maxLength = 40),
			@Valid(name = "sysMenu.path", maxLength = 100), @Valid(name = "sysMenu.menuLevel", maxLength = 3), @Valid(name = "sysMenu.hrefUrl", maxLength = 200),
			@Valid(name = "sysMenu.parentId", maxLength = 19), @Valid(name = "sysMenu.imgUrl", maxLength = 200), @Valid(name = "sysMenu.iconUrl", maxLength = 200),
			@Valid(name = "sysMenu.sort", maxLength = 10), @Valid(name = "sysMenu.delFlag", maxLength = 3), @Valid(name = "sysMenu.menuStatus", maxLength = 3),
			@Valid(name = "sysMenu.remark", maxLength = 255), @Valid(name = "sysMenu.isPublic", maxLength = 3), @Valid(name = "sysMenu.createId", maxLength = 10),
			@Valid(name = "sysMenu.createTime", maxLength = 19), @Valid(name = "sysMenu.updateId", maxLength = 10), @Valid(name = "sysMenu.updateTime", maxLength = 19), })
	@ApiImplicitParams({
			
			@ApiImplicitParam(name = "sysMenu.sysId", value = "系统id", dataType = "sysId", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.menuName", value = "菜单(模块)名", required = true, dataType = "menuName", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.menuCode", value = "菜单（模块）编号", required = true, dataType = "menuCode", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.path", value = "菜单层级路径", required = true, dataType = "path", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.menuLevel", value = "菜单级别", dataType = "menuLevel", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.hrefUrl", value = "菜单链接", dataType = "hrefUrl", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.parentId", value = "父级菜单id", dataType = "parentId", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.imgUrl", value = "菜单图片地址", dataType = "imgUrl", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.iconUrl", value = "菜单小图标 icon", dataType = "iconUrl", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.sort", value = "排序号", dataType = "sort", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.delFlag", value = "0:未删除 1:已删除", dataType = "delFlag", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.menuStatus", value = "是否冻结 0:未冰结 1已冻结", dataType = "menuStatus", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.remark", value = "模块描述,/备注", dataType = "remark", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.isPublic", value = "是否为公共模块（任务角色都能拥有的权限）", dataType = "isPublic", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.createId", value = "操作人", dataType = "createId", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.createTime", value = "创建时间", required = true, dataType = "createTime", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.updateId", value = "更新人", dataType = "updateId", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.updateTime", value = "更新时间", dataType = "updateTime", paramType = "query"), })
	public Result<SysMenu> addSysMenu(@Inject @ApiIgnore SysMenu sysMenu) {
		Result<SysMenu> result = new Result<SysMenu>();
		sysMenuService.insert(sysMenu);
		result.setData(sysMenu);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}*/
	
	@RequestMapping(value = "/addOrUpdateSysMenu", method = RequestMethod.POST)
	@ApiOperation(value = "6期-zhuangjianjian-更新或者增加菜单信息", notes = "更新或者增加菜单信息")
	@Valids({ @Valid(name = "sysMenu.id", maxLength = 19), @Valid(name = "sysMenu.sysId", maxLength = 19, required = true), @Valid(name = "sysMenu.menuName", maxLength = 120),
			@Valid(name = "sysMenu.menuCode", maxLength = 40), @Valid(name = "sysMenu.path", maxLength = 100), @Valid(name = "sysMenu.hrefUrl", maxLength = 200),
			@Valid(name = "sysMenu.parentId", maxLength = 19), @Valid(name = "sysMenu.imgUrl", maxLength = 200), @Valid(name = "sysMenu.iconUrl", maxLength = 200),
			@Valid(name = "sysMenu.sort", maxLength = 10), @Valid(name = "sysMenu.remark", maxLength = 255), @Valid(name = "sysMenu.isPublic", maxLength = 3),
			@Valid(name = "sysMenu.createId", maxLength = 10), @Valid(name = "sysMenu.isCommon", required = true) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "sysMenu.id", value = "主键 自增长", required = false, dataType = "id", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.sysId", value = "系统ids集", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.menuName", value = "菜单(模块)名", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.menuCode", value = "菜单（模块）编号", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.hrefUrl", value = "菜单链接", dataType = "hrefUrl", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.parentId", value = "父级菜单id", dataType = "parentId", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.imgUrl", value = "菜单图片地址", dataType = "imgUrl", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.iconUrl", value = "菜单小图标 icon", dataType = "iconUrl", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.sort", value = "排序号（序号）", dataType = "sort", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.remark", value = "模块描述,/备注", dataType = "remark", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.updateId", required = true, value = "操作人", dataType = "updateId", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.isCommon", value = "是否通用  1通用  0不通用", required = true, dataType = "integer", paramType = "query"), })
	public Result<SysMenu> addOrUpdateSysMenu(@Inject @ApiIgnore SysMenu sysMenu) {
		
		Result<SysMenu> result = sysMenuService.addOrUpdateSysMenu(sysMenu);
		
		return result;
	}
	
	@RequestMapping(value = "/delMenu", method = RequestMethod.POST)
	@ApiOperation(value = "6期-zhuangjiajian-根据主键删除菜单", notes = "删除菜单")
	public Result<SysMenu> delMenu(@ApiParam(required = true, name = "id", value = "主键 自增长") @RequestParam Long id) {
		Result<SysMenu> result = new Result<SysMenu>();
		sysMenuService.delete(id);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/seachSysMenu", method = RequestMethod.POST)
	@ApiOperation(value = "6期-zhuangjianjian-搜索菜单SKU菜单并分页", notes = "搜索菜单并分页")
	@Valids({
			
			@Valid(name = "sysMenu.sysId", maxLength = 19), @Valid(name = "sysMenu.menuName", maxLength = 120), @Valid(name = "sysMenu.menuCode", maxLength = 40),
			@Valid(name = "sysMenu.path", maxLength = 100), @Valid(name = "sysMenu.menuLevel", maxLength = 3), @Valid(name = "sysMenu.hrefUrl", maxLength = 200),
			@Valid(name = "sysMenu.parentId", maxLength = 19), @Valid(name = "sysMenu.imgUrl", maxLength = 200), @Valid(name = "sysMenu.iconUrl", maxLength = 200),
			@Valid(name = "sysMenu.sort", maxLength = 10), @Valid(name = "sysMenu.delFlag", maxLength = 3), @Valid(name = "sysMenu.menuStatus", maxLength = 3),
			@Valid(name = "sysMenu.remark", maxLength = 255), @Valid(name = "sysMenu.isPublic", maxLength = 3), @Valid(name = "sysMenu.createId", maxLength = 10),
			@Valid(name = "sysMenu.createTime", maxLength = 19), @Valid(name = "sysMenu.updateId", maxLength = 10), @Valid(name = "sysMenu.updateTime", maxLength = 19),
			@Valid(name = "page.pageNumber", maxValue = 999), @Valid(name = "page.pageSize", maxValue = 999), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "sysMenu.id", value = "菜单ID", dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.sysId", value = "系统id", dataType = "sysId", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.menuName", value = "菜单(模块)名", required = false, dataType = "menuName", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.menuCode", value = "菜单（模块）编号", required = false, dataType = "menuCode", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.path", value = "菜单层级路径", required = false, dataType = "path", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.menuLevel", value = "菜单级别", dataType = "menuLevel", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.hrefUrl", value = "菜单链接", dataType = "hrefUrl", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.parentId", value = "父级菜单id", dataType = "parentId", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.imgUrl", value = "菜单图片地址", dataType = "imgUrl", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.iconUrl", value = "菜单小图标 icon", dataType = "iconUrl", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.sort", value = "排序号", dataType = "sort", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.delFlag", value = "0:未删除 1:已删除", dataType = "delFlag", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.menuStatus", value = "是否冻结 0:未冰结 1已冻结", dataType = "menuStatus", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.remark", value = "模块描述,/备注", dataType = "remark", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.isPublic", value = "是否为公共模块（任务角色都能拥有的权限）", dataType = "isPublic", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.createId", value = "操作人", dataType = "createId", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.createTime", value = "创建时间", required = false, dataType = "createTime", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.updateId", value = "更新人", dataType = "updateId", paramType = "query"),
			@ApiImplicitParam(name = "sysMenu.updateTime", value = "更新时间", dataType = "updateTime", paramType = "query"),
			@ApiImplicitParam(name = "page.pageNumber", value = "页码", required = true, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "page.pageSize", value = "单页记录数", required = true, dataType = "integer", paramType = "query"), })
	public Result<List<SysMenu>> seachSysMenu(@Inject @ApiIgnore SysMenu sysMenu, @Inject @ApiIgnore Pagination page) {
		PageResult<List<SysMenu>> result = new PageResult<List<SysMenu>>();
		List<SysMenu> list = sysMenuService.seachSysMenu(sysMenu, page);
		
		// 填充父级菜单名称
		for (SysMenu s : list) {
			SysMenu parentMenu = sysMenuService.findById(s.getParentId());
			if (parentMenu != null) {
				s.setParentName(parentMenu.getMenuName());
			} else {
				s.setParentName(SysMenu.ROOT_MENU_NAME);
			}
		}
		
		result.setData(list);
		result.setTotal_record(page.getTotalRecord());
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/getMenuTree", method = RequestMethod.POST)
	@ApiOperation(notes = "查询菜单树.", value = "查询菜单树-chenjunjie")
	@Valids({ @Valid(name = "searchMode", maxValue = 1) })
	@ApiImplicitParams({
			@ApiImplicitParam(name = "searchMode", value = "可以不填（不填默认0）或者，只允许输入0或1， 查询模式 searchMode=0 只查询出有权限的模块（菜单） searchMode=1 查询所有模块（菜单），有权限hasPermission=1，无权限hasPermission=0", required = false, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "isCommon", value = "是否通用 1通用  0不通用", required = false, dataType = "integer", paramType = "query"), })
	
	public Result<List<MenuTreeNode>> getMenuTree(@RequestParam(required = false) Integer isCommon,
			@ApiParam(required = false, name = "userId", value = "用户ID") @RequestParam(required = false) Long userId,
			@ApiParam(required = false, name = "roleId", value = "角色ID") @RequestParam(required = false) Long roleId,
			@ApiParam(required = false, name = "sysId", value = "系统ID集合 逗号隔开") @RequestParam(required = false) String sysId,
			@ApiParam(required = false, name = "searchMode", value = "可以不填（不填默认0）或者，只允许输入0或1， 查询模式 searchMode=0 只查询出有权限的模块（菜单） searchMode=1 查询所有模块（菜单），有权限hasPermission=1，无权限hasPermission=0 ") @RequestParam(required = false) Integer searchMode) {
		PageResult<List<MenuTreeNode>> result = new PageResult<List<MenuTreeNode>>();
		if (searchMode == null) {
			// 默认使用 只查询出有权限的模块（菜单）
			searchMode = SysMenu.SEARCH_MODE_ONLY_SHOW;
		}
		result.setData(sysMenuService.getMenuTree(userId, roleId, sysId, searchMode, isCommon));
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	/**
	 * @date  2017-9-18 14:41:31
	 * @author zhuangjiajian
	 * @param id
	 * @param status
	 * @return
	 */
	@RequestMapping(value = "/modifyMenuStatus", method = RequestMethod.GET)
	@ApiOperation(value = "6期-zhuangjiajian-更改菜单状态", notes = "更改菜单状态")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "主键ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "updateId", value = "操作者ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "status", value = "0|1   0启用 1禁用", dataType = "integer", paramType = "query") })
	public Result modifyMenuStatus(@RequestParam(required = true) Long updateId, @RequestParam(required = true) Long id, @RequestParam(required = true) Integer status) {
		Result result = new Result();
		SysMenu sysMenu = sysMenuService.findById(id);
		String msg = "";
		if (sysMenu.getMenuStatus().equals(SysMenu.ENABLE)) {
			msg = "启用";
		} else {
			msg = "禁用";
		}
		if (sysMenu.getMenuStatus().equals(status)) {
			result.setErrorCode(ErrorCode.MODITY_STATUS_FAIL);
			result.setMessage("状态已经" + msg + "不能重复" + msg);
			return result;
		}
		SysMenu modifyMenu = new SysMenu();
		modifyMenu.setId(id);
		modifyMenu.setMenuStatus(status);
		sysMenuService.update(modifyMenu);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("菜单状态已改变成功");
		return result;
	}
	
	/**
	 * @date  2017-9-18 14:41:31
	 * @author zhuangjiajian
	 * @param id
	 * @param status
	 * @return
	 */
	@RequestMapping(value = "/setMenuPublicStatus", method = RequestMethod.GET)
	@ApiOperation(value = "6期-zhuangjiajian-设置菜单是否为公共模块", notes = "设置菜单是否为公共模块")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "主键ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "updateId", value = "操作者ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "isPublic", value = "0|1   0公共 1非公共", dataType = "integer", paramType = "query") })
	public Result setMenuPublicStatus(@RequestParam(required = true) Integer updateId, @RequestParam(required = true) Long id, @RequestParam(required = true) Integer isPublic) {
		Result result = new Result();
		SysMenu sysMenu = sysMenuService.findById(id);
		String msg = "";
		if (sysMenu.getIsPublic().equals(SysMenu.IS_PUBLIC)) {
			msg = "公开";
		} else {
			msg = "私有";
		}
		if (sysMenu.getIsPublic().equals(isPublic)) {
			result.setErrorCode(ErrorCode.MODITY_STATUS_FAIL);
			result.setMessage("模块已为" + msg + "状态,不能重复" + msg);
			return result;
		}
		SysMenu modifyMenu = new SysMenu();
		
		modifyMenu.setId(id);
		modifyMenu.setIsPublic(isPublic);
		modifyMenu.setUpdateId(updateId);
		modifyMenu.setUpdateTime(DateUtils.getCurrentDateTime());
		sysMenuService.update(modifyMenu);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("设置成功");
		return result;
	}
	
	@Valids({ @Valid(name = "permissionVo.updateId", required = true), @Valid(name = "permissionVo.permissions.m", required = false),
			@Valid(name = "permissionVo.permissions.r", required = false), @Valid(name = "permissionVo.roleId", required = true) })
	@ResponseBody
	@RequestMapping(value = "/setPermission", method = RequestMethod.POST)
	@ApiOperation(value = "权限管理-权限设置--chenjunjie", notes = "权限设置")
	public Result setPermission(@RequestBody ModityPermissionVo permissionVo) {
		Result result = sysMenuService.setPermission(permissionVo);
		return result;
	}
}
